package com.fare.controller.auth;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.openkoala.auth.application.RoleApplication;
import org.openkoala.auth.application.vo.RoleVO;
import org.openkoala.koala.auth.ss3adapter.CustomUserDetails;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dayatang.domain.InstanceFactory;
import com.fare.fdc.action.UserCareLineAction;
import com.fare.fdc.listener.UserOnlineListener;
import com.fare.taluo.action.system.UserInfoLogAction;
import com.fare.taluo.application.plane.city.CityQueryApplication;
import com.fare.taluo.application.plane.mycare.UserCareLineApplication;
import com.fare.taluo.auth.security.ContextLoginUser;
import com.fare.taluo.auth.security.LogonUserCertificate;
import com.fare.taluo.vo.auth.UserInfoLoginVO;
import com.fare.taluo.vo.config.city.CityLineSelectVO;

@Controller
public class BaseController {
	
	@Inject
	private RoleApplication roleApplication;
	
	@RequestMapping("/login")
	public String login() {
		return "login";
	}
	@ResponseBody
	@RequestMapping("/language")
	public Map<String,Object> language(HttpServletRequest request) {
		String language = request.getParameter("language");
		//设置当前用户上下文语言
		request.getSession().setAttribute("currentLanguage", language);
		Locale local = new Locale("zh", "CN");
		if(language.equalsIgnoreCase("english")){
			local = new Locale("en", "US");
		}
		request.getSession().setAttribute("WW_TRANS_I18N_LOCALE", local);
		//返回OK
		Map<String, Object> dataMap = new HashMap<String,Object>();
		dataMap.put("result", "ok");
		return dataMap;
	}
	@RequestMapping("/index")
	public String index(HttpServletRequest request) {
		CustomUserDetails current = (CustomUserDetails) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		//在此插入log信息
		UserInfoLoginVO vo =new UserInfoLoginVO();
		LogonUserCertificate currentUser = ContextLoginUser.getLoginUser();
		//把用户信息保存在监听器中
		UserOnlineListener UOUser=new UserOnlineListener();
		UOUser.setUser(currentUser);
		if(currentUser !=null){
			vo.setUserName(currentUser.getLoginId());
			vo.setCreateTime(new Date());
			vo.setLoginTime(new Date());
			vo.setIP(com.fare.taluo.util.IPUtil.ipClient(request));
		}
		InstanceFactory.getInstance(UserInfoLogAction.class).inputLog(vo);
		if (current.isSuper()) {
			return "index";
		}
		
		if(currentUser.getRoleId() != null){
			RoleVO roleVO = roleApplication.getRole(currentUser.getRoleId());
			String roleUrl = roleVO.getRoleDesc();
			if("index".equals(roleUrl)){
				return "index";
			}else{
				if(!InstanceFactory.getInstance(UserCareLineApplication.class).hasCareLine(currentUser.getLocalUserId())){
					//如果没有关注航线，则直接进入首页
					int tempLines=0;
//					List<CityLineSelectVO> tempLineList=InstanceFactory.getInstance(CityQueryApplication.class).getCitysByChannel(currentUser.getSourceKey());
					tempLines=InstanceFactory.getInstance(CityQueryApplication.class).getCitysByChannelLines(currentUser.getSourceKey());
					if(tempLines ==0) return "redirect:"+roleUrl;
					else {
						return "redirect:/usercareline/registerView.action?hbaseKey="+currentUser.getSourceKey();
					}
					
				}
				return "redirect:"+roleUrl;
			}
		}
		return "index";
	}

}
